/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jdesktop.wonderland.modules.printscreenplugin.client;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.wonderland.client.hud.CompassLayout;
import org.jdesktop.wonderland.client.hud.HUD;
import org.jdesktop.wonderland.client.hud.HUDComponent;
import org.jdesktop.wonderland.client.hud.HUDManagerFactory;
import org.jdesktop.wonderland.client.login.ServerSessionManager;

/**
 *
 * @author Dominik
 */
public class PrintScreenListener extends KeyAdapter {
    
    private TakePrintScreen printScreenSaver;
    private PrintScreenInfo infoPanel;
    private HUDComponent hudComponent = null;
    
    public PrintScreenListener(ServerSessionManager ss)
    {
        printScreenSaver = new TakePrintScreen(ss);
        infoPanel = new PrintScreenInfo();
    }
    
    private void createInfoPanel()
    {
        //Info Panel dem Main HUD hinzufügen
        HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
        hudComponent = mainHUD.createComponent(infoPanel);
        hudComponent.setName("Info Message");
        hudComponent.setPreferredLocation(CompassLayout.Layout.SOUTH);
        mainHUD.addComponent(hudComponent);
    }
    
    private void displayFiveSeconds()
    {       
        hudComponent.setVisible(true);
        new HUDCloser().start();
    }
    
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_O)
        {
            if(printScreenSaver.takeScreenshot())
            {
                infoPanel.setInfoText("Screenshot was saved");
            }
            else
            {
                infoPanel.setInfoText("Screenshot could not be saved");
            }
            
            if(hudComponent == null)
            {
                createInfoPanel();
            }
            
            displayFiveSeconds();
        }
    }

    /**
     * @return the printScreenSaver
     */
    public TakePrintScreen getPrintScreenSaver() {
        return printScreenSaver;
    }

    /**
     * @param printScreenSaver the printScreenSaver to set
     */
    public void setPrintScreenSaver(TakePrintScreen printScreenSaver) {
        this.printScreenSaver = printScreenSaver;
    }
    
    private class HUDCloser extends Thread
    {
        @Override
        public void run()
        {
            try {
                this.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(PrintScreenListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            hudComponent.setVisible(false);
        }
    }
    
}
